<?php
namespace Tongcheng\Controller;
use Think\Controller;
class DocDicBackController extends CommonController {
    public function index(){
        $model = M("Docdic");
        $map = array();
        // $map['is_pay'] = 1;

        $res['count'] = $model->where($map)->count();
        $page = $_GET['page']?$_GET['page']:1;
        $limit = $_GET['limit']?$_GET['limit']:10;
        $list = $model->field("tp_docdic.*,FROM_UNIXTIME(tp_docdic.add_time,\"%m-%d %H:%i\") time")->where()->page("{$page},{$limit}")->order('id desc')->select();
        $res['list']= $list;


        if(IS_AJAX){
            $this->ajaxReturn($res);
        }else{
            $this->assign('res', $res);
            $this->display();
        }
    }
    public function tui(){
        $r = tui("coupon",I('id'),session('wid'),$_POST['status']);
        if($r){
            $this->success("操作成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    public function getClist(){
        $model = M('Docdic');
        $pid = I('pid');
        if(empty($pid)){
            $this->ajaxReturn(0);
        }
        $clist = $model->field("tp_docdic.*")->where('pid = '.$pid.' and cid = 0')->select();
        if(empty($clist)){
           $this->ajaxReturn(0);
        }else{
            $this->ajaxReturn($clist,'json');
        }
    }

    public function add(){
        $model = M("Docdic");
        $plist = $model->field("tp_docdic.*")->where("pid = 0")->order('id asc')->select();
        $this->assign('plist', $plist);
        $this->display();
    }

    public function edit(){
        $model = M("Docdic");
        $id = $_POST['id'] == 'undefined'?'':$_POST['id'];
        $isadd = 0;
        if(empty($id)){
            $id = $_GET['id'] == 'undefined'?'':$_GET['id'];
            if(!empty($id) && 'undefined' != $id){
                $map['id']=$id;
            }else{
                $isadd = 1;
            }
        }else{
            $map['id'] = $id;
            $isadd = 2;
        }
        $list = $model->field('tp_docdic.*')->where($map)->find();
        $type = $_POST['type'];
        $data['type'] = $_POST['type'];
        $data['value'] = $_POST['value'];
        $data['name'] = $_POST['name'];
        $data['pid'] = $_POST['pid'];
        $data['cid'] = $_POST['cid'];
        if(empty($data['value'])){
            $isadd = 0;
        }
        $data['add_time'] = time();
        if(1==$isadd){

            //新增
            $model->add($data);
            $this->ajaxReturn(1);
        }else if(2 == $isadd){
            //更新
            $model->where('id = '.$id)->save($data);
            $this->ajaxReturn(1);
        }
        $this->assign('list', $list);
        $plist = $model->field("tp_docdic.*")->where("pid = 0")->order('id asc')->select();
        $this->assign('plist', $plist);
        $this->display();
    }

    /**
     * 删除
     */
    public function del(){
        $id = I('id');
        if(empty($id)){
            $this->ajaxReturn(0);

        }
        $result = M("Docdic")->where('id = '.$id)->delete();
        if($result){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }
}